package wwk.com.android_meilishuo.mvp.model;

import android.content.Context;
import android.os.Handler;

import com.squareup.okhttp.Callback;
import com.squareup.okhttp.Request;
import com.squareup.okhttp.Response;
import com.wwk.core.net.Constants;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


import retrofit2.Retrofit;
import retrofit2.adapter.rxjava.RxJavaCallAdapterFactory;
import retrofit2.converter.gson.GsonConverterFactory;
import rx.Observable;
import rx.android.schedulers.AndroidSchedulers;
import rx.functions.Action1;
import rx.schedulers.Schedulers;
import wwk.com.android_meilishuo.interfaces.MLSInterface;
import wwk.com.android_meilishuo.ui.home.bean.ChannelBean;
import wwk.com.android_meilishuo.ui.home.bean.FashionTitleBean;
import wwk.com.android_meilishuo.ui.home.bean.HomeHeadBean;
import wwk.com.android_meilishuo.ui.home.bean.ListBean;
import wwk.com.android_meilishuo.ui.home.bean.TrendBean;
import wwk.com.android_meilishuo.utils.OkHttpClientUtils;

/**
 * Created by Administrator on 2016/12/29.
 */

public class HomeHeadBiz implements MLSModel.IHomeHeadBiz {
    private MLSInterface mMLSInterface;

    @Override
    public void loadHomeHeadData(final MLSModel.OnHomeHeadListener onHomeHeadListener, Context context) {
        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl(Constants.BASE_URL_MCE)
                .addConverterFactory(GsonConverterFactory.create())
                .addCallAdapterFactory(RxJavaCallAdapterFactory.create())
                .build();
        mMLSInterface = retrofit.create(MLSInterface.class);
        Observable observable = mMLSInterface.getHHData();
        observable.subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Action1<HomeHeadBean>() {

                    @Override
                    public void call(HomeHeadBean homeHeadBean) {
                        List<ChannelBean.ListBean> listBeen = homeHeadBean.getData().getChannel().getList();
                        List<FashionTitleBean.ListBean> fashionTitleBeen = homeHeadBean.getData().getFashionTitle().getList();
                        onHomeHeadListener.onHeadSuccess(listBeen);
                        onHomeHeadListener.onFashionSuccess(fashionTitleBeen);
                    }
                });

    }

    private Handler handler = new Handler();

    @Override
    public void loadBeautyHeadData(final MLSModel.OnBeautyHeadListener onBeautyHeadListener, Context context) {

        OkHttpClientUtils.getDataAsync(context, Constants.URL_MCE, new Callback() {
            @Override
            public void onFailure(Request request, IOException e) {

            }

            @Override
            public void onResponse(Response response) throws IOException {
                final List<ListBean> list_banner = new ArrayList<ListBean>();
                final List<ListBean> list_entry = new ArrayList<ListBean>();
                final List<ListBean> list_All = new ArrayList<ListBean>();

                String json = response.body().string();
                try {
                    JSONObject jsonObject = new JSONObject(json);
                    JSONObject jsonObject1 = jsonObject.optJSONObject("data");
                    JSONObject jsonObject_banner = jsonObject1.optJSONObject("1_3_banner");
                    JSONObject jsonObject_entry = jsonObject1.optJSONObject("1_4_entry");
                    JSONArray jsonArray_banner = jsonObject_banner.optJSONArray("list");
                    JSONArray jsonArray_entry = jsonObject_entry.optJSONArray("list");
                    for (int i = 0; i < jsonArray_banner.length(); i++) {
                        ListBean listBean_banner = new ListBean();
                        JSONObject jsonObject3 = jsonArray_banner.optJSONObject(i);
                        listBean_banner.setImage(jsonObject3.optString("image", ""));
                        list_banner.add(listBean_banner);
                    }
                    for (int i = 0; i < jsonArray_entry.length(); i++) {
                        ListBean listBean_entry = new ListBean();
                        JSONObject object = jsonArray_entry.optJSONObject(i);
                        listBean_entry.setImage(object.optString("image", ""));
                        list_entry.add(listBean_entry);
                    }
                    list_All.addAll(list_banner);
                    list_All.addAll(list_entry);
                    handler.post(new Runnable() {
                        @Override
                        public void run() {
                            onBeautyHeadListener.onBeautySuccess(list_All);
                        }
                    });
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        }, "");

        /*Retrofit retrofit = new Retrofit.Builder()
                .baseUrl(Constants.BASE_URL_MCE)
                // .addConverterFactory(GsonConverterFactory.create())
                .addCallAdapterFactory(RxJavaCallAdapterFactory.create())
                .build();
        mMLSInterface = retrofit.create(MLSInterface.class);
        Observable observable = mMLSInterface.getBeautyData();
        observable.subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Action1<ListBean>() {
                    @Override
                    public void call(ListBean listBean) {
                        Log.i("eeeeeeeeeeeeeeee", listBean.toString());
                        List<ListBean> list=new ArrayList<ListBean>();
                        String json = listBean.toString();
                        try {
                            JSONObject jsonObject = new JSONObject(json);
                            JSONObject jsonObject1 = jsonObject.optJSONObject("data");
                            JSONObject jsonObject2 = jsonObject1.optJSONObject("1_3_banner");
                            JSONArray jsonArray = jsonObject2.optJSONArray("list");
                            for (int i = 0; i < jsonArray.length(); i++) {
                                ListBean listBean1 = new ListBean();
                                JSONObject jsonObject3=jsonArray.optJSONObject(i);
                                listBean.setImage(jsonObject3.optString("image",""));
                                list.add(listBean);
                                onHomeHeadListener.onBeautySuccess(list);
                            }
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }
                });*/
    }

    @Override
    public void loadTrendHeadData(final MLSModel.OnTrendHeadListener onTrendHeadListener, Context context) {
        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl(Constants.BASE_URL_CLJX)
                .addConverterFactory(GsonConverterFactory.create())
                .addCallAdapterFactory(RxJavaCallAdapterFactory.create())
                .build();
        mMLSInterface = retrofit.create(MLSInterface.class);
        Map<String, String> map = new HashMap<>();
        map.put("limit", "6");
        Observable observable = mMLSInterface.getTrendData("android", map);
        observable.subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Action1<TrendBean>() {
                    @Override
                    public void call(TrendBean trendBean) {
                        List<TrendBean.DataBean.ListBean> listBeen = trendBean.getData().getList();
                        TrendBean.DataBean.TomorrowAdvanceBean tomorrowAdvanceBeen = trendBean.getData().getTomorrowAdvance();
                        onTrendHeadListener.onTrendSuccess(listBeen, tomorrowAdvanceBeen);
                    }
                });
    }
}
